package com.spring.gop.proj.controller;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;

import com.spring.gop.proj.interfaces.QnaService;
import com.spring.gop.proj.model.Qna;
import com.spring.gop.proj.model.Cdt;
import com.spring.gop.proj.model.User;

@Controller
@RequestMapping("/help/qna/qna_admin/*")
@SessionAttributes("qna")
public class QnaController {

	@Autowired private QnaService qnaService;
	
	@RequestMapping(value="/list", method=RequestMethod.GET)
	public void listForm(Model model) {
			
		model.addAttribute("qna", qnaService.getArticles());
	}
	
	@RequestMapping(value="/list", method=RequestMethod.POST)
	public void list(Model model, String searchWhat, String searchText){
		System.out.println(searchWhat);
		System.out.println(searchText);
		Cdt cdt = new Cdt();
		cdt.setWhat(searchWhat);
		cdt.setContent(searchText);
		System.out.println(qnaService.getArticles2(cdt).size());

		model.addAttribute("qna", qnaService.getArticles2(cdt));
	}
	
	@RequestMapping(value="/view", method=RequestMethod.GET)
	public Qna view(int q_num) {
		return qnaService.getArticle(q_num);
	}
		
	@RequestMapping(value="/insert", method=RequestMethod.GET)
	public void insertForm(){}
	
	@RequestMapping(value="/insert", method=RequestMethod.POST)
	public String insert(HttpSession session, @ModelAttribute("newArticle") Qna qna){
		
		qna.setEmail1("admin");
		qna.setEmail2("primus.com");
		qna.setContactnum1("02");
		qna.setContactnum2("1234");
		qna.setContactnum3("5678");
		
		User user = (User)session.getAttribute("user");
		qna.setMember_id(user.getMember_id());
		qna.setMember_name(user.getMember_name());
		
		qnaService.insertArticle(qna);
		return "redirect:list";
	}
	
	
	
	@RequestMapping(value="/update", method=RequestMethod.GET)
	public void updateForm(){}
	
	@RequestMapping(value="/update", method=RequestMethod.POST)
	public String update(@ModelAttribute("updateArticle") Qna qna, SessionStatus session){
		qnaService.updateArticle(qna);
		session.setComplete();
		return "redirect:list";
	}
	
	@RequestMapping(value="/delete",method=RequestMethod.GET)
	public String delete(int q_num, String pass, SessionStatus session){
		int check=qnaService.delArticle(q_num, pass);
		//qnaService.delArticle(q_num, pass);
		if(check==1){
			session.setComplete();
			return "redirect:list";
		}else{
			return "redirect:list";
		}
	}
	
	
	
	
	// 광고 및 제휴문의 컨트롤러
	
	@RequestMapping(value="/list1", method=RequestMethod.GET)
	public void listForm1(Model model) {
			
		model.addAttribute("qna", qnaService.getArticles3());
	}
	
	@RequestMapping(value="/list1", method=RequestMethod.POST)
	public void list1(Model model, String searchWhat, String searchText){
		System.out.println(searchWhat);
		System.out.println(searchText);
		Cdt cdt = new Cdt();
		cdt.setWhat(searchWhat);
		cdt.setContent(searchText);
		System.out.println(qnaService.getArticles5(cdt).size());

		model.addAttribute("qna", qnaService.getArticles5(cdt));
	}
	
	@RequestMapping(value="/view1", method=RequestMethod.GET)
	public Qna view1(int q_num) {
		return qnaService.getArticle1(q_num);
	}
		
	@RequestMapping(value="/insert1", method=RequestMethod.GET)
	public void insertForm1(){}
	
	@RequestMapping(value="/insert1", method=RequestMethod.POST)
	public String insert1(HttpSession session, @ModelAttribute("newArticle") Qna qna){
		
		qna.setCpr_company("프리머스");
		qna.setCpr_kind("해당없음");
		qna.setEmail1("admin");
		qna.setEmail2("primus.com");
		qna.setContactnum1("02");
		qna.setContactnum2("1234");
		qna.setContactnum3("5678");
		
		User user = (User)session.getAttribute("user");
		qna.setMember_id(user.getMember_id());
		qna.setMember_name(user.getMember_name());
		
		qnaService.insertArticle1(qna);
		return "redirect:list";
	}
	
	
	
	@RequestMapping(value="/update1", method=RequestMethod.GET)
	public void updateForm1(){}
	
	@RequestMapping(value="/update1", method=RequestMethod.POST)
	public String update1(@ModelAttribute("updateArticle1") Qna qna, SessionStatus session){
		qnaService.updateArticle1(qna);
		session.setComplete();
		return "redirect:list";
	}
	
	@RequestMapping(value="/delete1",method=RequestMethod.GET)
	public String delete1(int q_num, String pass, SessionStatus session){
		int check=qnaService.delArticle1(q_num, pass);
		//qnaService.delArticle(q_num, pass);
		if(check==1){
			session.setComplete();
			return "redirect:list";
		}else{
			return "redirect:list";
		}
	}
}